# from . import confWrap
# from . import logConfig
import logging.config
from .date_util import *
from .anotations import time_log
from .expressions import *
from .abtest import *

# def get_config():
#     return confWrap.CONF()


# def get_logger():
#     logging.config.dictConfig(logConfig.LOGGING)
#     return logging.getLogger("loggers")


def Singleton(cls):
    _instance = {}

    def _singleton(*args, **kargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kargs)
        return _instance[cls]

    return _singleton


# def print_time_log(func_name, s, risk_order_id='none'):
#     e = get_timestamp()
#     if get_config().get('default', 'show_time_log') == 'true':
#         log_template = "time_log:%s|%s|%s" % (risk_order_id, func_name, int(e - s))
#         get_logger().info(log_template)
#     return e


ID_Demilter = '#'


def get_risk_order_id(request_json):
    """
    获取id
    :param request_json:
    :return:
    """
    return request_json['country'] + ID_Demilter \
           + (str)(request_json['merchantId']) + ID_Demilter \
           + request_json['packageId'] + ID_Demilter \
           + request_json['channelId'] + ID_Demilter \
           + request_json['loanOrderId']


def get_risk_user_id(request_json):
    """
    获取id
    :param request_json:
    :return:
    """
    return request_json['country'] + ID_Demilter \
           + (str)(request_json['merchantId']) + ID_Demilter \
           + request_json['appId'] + ID_Demilter \
           + request_json['channel'] + ID_Demilter \
           + request_json['userId']


def get_fileline_to_list(file_path):
    with open(file_path, 'r') as f:
        result = list()
        for line in f.readlines():
            result.append(line.strip())
    return result


def split_risk_order_id(risk_id):
    """
    MEXICO#1#com.goprestamo.mexico#81fc882b-c487-4af9-adfe-853bf32a97f1#534e440d-75d8-4831-a7f9-9771e4387083
    :param risk_id:
    :return:
    """
    try:
        l = risk_id.strip().split('#')
        merchant_id = int(l[1])
        country = l[0]
        package_id = l[2]
        channel_id = l[3]
        bis_order_id = l[4]
        return {
            'MERCHANT_ID': merchant_id,
            'COUNTRY': country,
            'PACKAGE_ID': package_id,
            'CHANNEL_ID': channel_id,
            'BIS_ORDER_ID': bis_order_id
        }
    except:
        return {
            'MERCHANT_ID': 1,
            'COUNTRY': 'MEXICO',
            'PACKAGE_ID': 'NONE',
            'CHANNEL_ID': 'NONE',
            'BIS_ORDER_ID': 'NONE'
        }


def split_risk_user_id(risk_id):
    """
    MEXICO#1#com.goprestamo.mexico#81fc882b-c487-4af9-adfe-853bf32a97f1#1442244740090298368
    :param risk_id:
    :return:
    """
    try:
        l = risk_id.strip().split('#')
        merchant_id = int(l[1])
        country = l[0]
        package_id = l[2]
        channel_id = l[3]
        bis_user_id = l[4]
        return {
            'MERCHANT_ID': 1,
            'COUNTRY': country,
            'PACKAGE_ID': package_id,
            'CHANNEL_ID': channel_id,
            'BIS_USER_ID': bis_user_id
        }
    except:
        return {
            'MERCHANT_ID': 1,
            'COUNTRY': 'MEXICO',
            'PACKAGE_ID': 'NONE',
            'CHANNEL_ID': 'NONE',
            'BIS_ORDER_ID': 'NONE'
        }

if __name__ == '__main__':
    print(split_risk_order_id('MEXICO#1#com.goprestamo.mexico#81fc882b-c487-4af9-adfe-853bf32a97f1#534e440d-75d8-4831-a7f9-9771e4387083'))
    print(split_risk_user_id('MEXICO#1#com.goprestamo.mexico#81fc882b-c487-4af9-adfe-853bf32a97f1#1442244740090298368'))